var ctx;
var keyword = "";
var houseType;
var houseState;
var regionId;
var pSize;
var selectRu = [];
var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引

function setCtx(ctx) {
	this.ctx = ctx;
}

function gotoPage(page) {
	keyword = ifNull($("#keyword").val());
	houseType = ifNull($("#houseType").val());
	houseState = ifNull($("#houseState").val());
	regionId = ifNull($("#regionId").val());
	pSize = ifNull($("#pSize").val());
	// 解决ie9以下placeholder与value重复的问题
	if (keyword == $("#keyword").attr("placeholder")) {
		keyword = "";
	}

	loadContent("/cellHouse/cellHouseList?pNo=" + page + "&keyword=" + keyword
			+ "&houseType=" + houseType + "&houseState=" + houseState
			+ "&regionId=" + regionId + "&pSize="+ pSize);
}

function ifNull(mess) {
	if (mess == null || mess == undefined) {
		mess = "";
	}

	return mess;
}

// 条件搜索用户列表
function getCellHouseList() {
	keyword = ifNull($("#keyword").val());
	houseType = ifNull($("#houseType").val());
	houseState = ifNull($("#houseState").val());
	regionId = ifNull($("#regionId").val());
	pSize = ifNull($("#pSize").val());
	// 解决ie9以下placeholder与value重复的问题
	if (keyword == $("#keyword").attr("placeholder")) {
		keyword = "";
	}

	loadContent("/cellHouse/cellHouseList?pNo=1&keyword=" + keyword + "&houseType="
			+ houseType + "&houseState=" + houseState + "&regionId=" + regionId + "&pSize="+ pSize);

}

function gotoCellHousePage(page) {
	if (page == null) {
		page = $(".inputclass").val();
	}
	keyword = ifNull($("#keyword").val());
	houseType = ifNull($("#houseType").val());
	houseState = ifNull($("#houseState").val());
	regionId = ifNull($("#regionId").val());
	pSize = ifNull($("#pSize").val());
	// 解决ie9以下placeholder与value重复的问题
	if (keyword == $("#keyword").attr("placeholder")) {
		keyword = "";
	}

	loadContent("/cellHouse/cellHouseList?pNo=" + page + "&keyword=" + keyword
			+ "&houseType=" + houseType + "&houseState=" + houseState
			+ "&regionId=" + regionId + "&pSize="+ pSize);
}

function detailCellHouseInfo(id) {
	loadContent("/cellHouse/cellHouseDetail?id=" + id + "");
}

// 新增或编辑用户(1:新增 2:编辑)
function saveOrUpdate(id) {
	if (id != null) {
		loadContent("/cellHouse/editCellHouse?id=" + id + "");
	} else {
		loadContent("/cellHouse/editCellHouse?id=0");
	}

}

// 全选和反选
function selectAll(target) {
	if (target.checked) {
		$("#tableTbody :checkbox").attr("checked", true);
		$(target).parents("thead").siblings("tbody").children().children()
				.each(function() {
					$(this).css("background", "#D3F0F1");
				});
	} else {
		$("#tableTbody :checkbox").attr("checked", false);
		$(target).parents("thead").siblings("tbody").children().children()
				.each(function() {
					$(this).css("background", "");
				});
	}

}

// 鼠标滑入一行改变背景颜色
function mouseoverOne(target) {
	$(target).children().each(function() {
		$(this).css("background", "#D3F0F1");
	});
}

// 鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target) {
	$(target).children().each(function() {
		$(this).css("background", "");
	});
}

// 选择或取消选中当前记录
function selectOne(id, target) {
	if (target.checked) {
		$("#house" + id).attr("checked", true);
		$(target).parents("tr").children().each(function() {
			$(this).css("background", "#D3F0F1");
		});
	} else {
		$("#house" + id).attr("checked", false);
		$(target).parents("tr").children().each(function() {
			$(this).css("background", "");
		});
	}
}

function deleteByIds() {
	if ($("img[name='houseChecked'][src*=honggou]").length == 0) {
		layer.msg("请选择要删除的记录!", {
			icon : 3,
			time : 2000
		// 2秒关闭（如果不配置，默认是3秒）
		});
	} else {
		var delIds = [];
		$("#tableTbody img[name='houseChecked'][src*=honggou]").each(
				function() {
					delIds.push($(this).attr("class"));
				});
		layer.confirm('确认删除?', {
			icon : 3,
			title : '提示'
		}, function(index) {
			// do something
			$.post(ctx + "/cellHouse/deleteCellHouse", {
				houseIDs : delIds
			}, function(data) {
				data = JSON.parse(data);
				if (data.success) {
					layer.msg("删除成功", {
						icon : 1,
						time : 2000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						loadContent("/cellHouse/cellHouseList");
					});
				} else {
					layer.msg(data.msg, {
						icon : 2,
						time : 2000
					// 2秒关闭（如果不配置，默认是3秒）
					});
				}
			});
			layer.close(index);
		});

	}
}

function updateSort(houseId) {
	if (houseId != null) {
		$.get(ctx + "/cellHouse/updateSort", {
			id : houseId
		}, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("操作成功", {
					icon : 1,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					loadContent("/cellHouse/cellHouseList");
				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				});
			}
		});
	}
}

// 实现字段较长时的隐藏于显示
var index;
function mouseOverName(item, name) {
	index = layer.tips(name, '#name' + item, {
		tips : [ 3, '#37CED4' ]
	});
}

function mouseOutName(item, name) {
	layer.close(index);
}

//实现字段较长时的隐藏于显示
var index;
function mouseOverOName(item, oName) {
	index = layer.tips(oName, '#oName' + item, {
		tips : [ 3, '#37CED4' ]
	});
}

function mouseOutOName(item, oName) {
	layer.close(index);
}

/*房屋申请--审核--状态更新*/
function approve(id, state) {
	var message;
	var detailAddress = $("#detailAddress").val();
	var regionId = $("#regionId").val();
	var buildingId = $("#buildingId").val();
	var unitId = $("#unitId").val();
	var houseId = $("#houseId").val();
	var recepientName = $("#recepientName").val();
	var recepientPhone = $("#recepientPhone").val();
	
	switch (parseInt(state)) {
	case 2:
		message = "是否予以通过审核？";
		break;
	case 5:
		message = "是否不予以通过审核？";
		break;
	default:
		break;
	}
	layer.confirm(message, {
		icon : 3,
		title : '提示'
	}, function(index) {
		$.ajax({
			type : "POST",
			url : ctx + "/cellHouse/cellHouseUpdating",
			cache : false,
			dataType : 'json',
			data : {
				id : id,
				state : state,
				regionId :regionId,
				buildingId :buildingId,
				houseId :houseId,
				unitId :unitId,
				recepientName :recepientName,
				recepientPhone :recepientPhone,
				detailAddress : detailAddress
			},
			success : function(result) {
				if (result.success) {

					layer.msg("操作成功", {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						loadContent("/cellHouse/cellHouseApplication");
					});
				} else {
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
		layer.close(index);
	});
}

//条件搜索用户列表
function getCellHouseList1() {
	name = ifNull($("#name").val());
	houseType = ifNull($("#houseType").val());
	houseState = ifNull($("#houseState").val());
	regionId = ifNull($("#regionId").val());
	pSize = ifNull($("#pSize").val());
	// 解决ie9以下placeholder与value重复的问题
	if (name == $("#name").attr("placeholder")) {
		name = "";
	}

	loadContent("/cellHouse/cellHouseApplication?pNo=1&name=" + name + "&houseType="
			+ houseType + "&houseState=" + houseState + "&regionId=" + regionId + "&pSize="+ pSize);

}

//批量导入
function initCellHouse () {
	url = ctx + "/cellHouse/initList";
	msg = "批量导入";
    importCellHouseList(msg, url);
}

var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
//弹出导入小区房屋信息框
function importCellHouseList(msg,url){
	  //官网欢迎页
	   index = layer.open({
		     type: 2,
		     //skin: 'layui-layer-lan',
		     title: msg,
		     fix: false,
		     shadeClose: true,
		     scrollbar: false,
		     maxmin: false,
		     edit: true,
		     closeBtn: 1,
		     area: ['500px', '220px'],
		     content: url,
		     btn: ['设置', '取消']
			   ,yes: function(index, layero){ //或者使用btn1
			       //按钮【按钮一】的回调
				var iframeWin = window[layero.find('iframe')[0]['name']]; 
			   	iframeWin.importCellHouse();

			   },cancel: function(index){ //或者使用btn2
			       //按钮【按钮二】的回调
		}
	});
}

function importErrerAlert(msg){
//	alert(msg);
	var msgs = eval(msg);
	var alert = '';
	for(var i = 0 ; i < msgs.length ; i ++){
		alert = alert + msgs[i] + '</br>';
	}
	layer.alert(alert,{area: ['500px', '300px']}, function(index){
		loadContent("/cellHouse/cellHouseList");
		layer.closeAll();
	});
}


//导出小区房屋信息
function exportExcel() {
	total = ifNull($("#total").val());
	keyword = ifNull($("#keyword").val());
	houseType = ifNull($("#houseType").val());
	houseState = ifNull($("#houseState").val());
	regionId = ifNull($("#regionId").val());
	// 解决ie9以下placeholder与value重复的问题
	if (keyword == $("#keyword").attr("placeholder")) {
		keyword = "";
	}
	if(total >0) {
		location.href=ctx +"/cellHouse/exportCellHouseExcel?keyword=" + keyword + "&houseType="
			+ houseType + "&houseState=" + houseState + "&regionId=" + regionId;
	} else {
		layer.msg("未获取到数据，无法导出...",{
			icon: 2,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}
}